<template>
	<view class="box">
		<view class="form">
			<!-- 			<view class="form-item">
				<view class="">卡号类型</view>
				<view class="">在线充值</view>
			</view> -->
			<view class="form-item">
				<view class="">卡号</view>
				<input v-model="num" class="form-item-input" type="text" value="" placeholder="请输入充值卡号" />
			</view>
			<view class="form-item">
				<view class="">兑换口令</view>
				<input v-model="numPassword" class="form-item-input" type="text" value="" placeholder="请输入兑换口令" />
			</view>
		</view>
		<view class="u-flex">
			<view class="pay" @click="onPay">立即充值</view>
		</view>
		<view class="" style="color: red;font-size: 30rpx;">注意事项：</view>
		<view class="zy">
			<view class="zu-text">1. 用户充值成功后不予提现，充值的余额只能用于该商城购买产品。</view>
			<view class="zu-text">2. 充值成功后正常情况下会实时到账。如有疑问，请联系在线客服。</view>
			<view class="zu-text">3. 最终解释权在于本商城。</view>
		</view>
		<sww-login />
	</view>
</template>

<script>
	import {
		uniModal
	} from '@/utils/uniPromise';
	export default {
		data() {
			return {
				num: '',
				numPassword: ''
			};
		},
		methods: {
			$onShow() {
				this.$login();
			},
			onPay() {
				this.$login('onPay').then(() => {
					if (!this.num) {
						this.$toast('请输入卡号');
						return;
					}
					if (!this.numPassword) {
						this.$toast('请输入口令');
						return;
					}
					this.$resPost('GetElectronInfoByCode', {
						code: this.num,
						guid: this.numPassword
					}).then(res => {
						if (JSON.stringify(res.data) == '[]' || !res.data) {
							this.$toast('卡号或口令错误');
							return;
						}
						let obj = res.data[0];

						if (obj.status != 0) {
							this.$toast('该卡号已被使用');
							return;
						}

						if (obj.mode != '0') {
							this.$toast('不是充值卡号');
							return;
						}

						let params = {
							type: 2,
							pay_type: 1,
							amount: obj.price,
							remark: '卡号充值:' + this.num,
							order_type: 0,
							status: 1,
							order_id: 0
						};

						uniModal({
							content: `充值￥${obj.price}`
						}).then(() => {
							this.$resPost('AddCodeWriteOff', {
								code: this.num,
								business_id: undefined
							}).then(res3 => {
								if (res3.Status == 1) {
									this.$resPost('AddUsersConsume', params).then(res2 => {
										if (res2.Status == 1) {
											this.$resPost('AddCodeWriteOff', {
												code: this.num,
												business_id: undefined,
												order_no: res2.data,
											}).then(() => {
												this.num = '';
												this.$to(
													`/package/user/cardNum/succeeded/index?price=${obj.price}`
												);
											})

										}
									});
								}
							});
						});
					});
				});
			}
		}
	};
</script>

<style scoped lang="scss">
	.box {
		padding: 26rpx;
	}

	.form {
		font-size: 30rpx;

		.form-item {
			display: flex;
			justify-content: space-between;
			padding: 30rpx 0;
			border-bottom: 1rpx solid #f5f5f5;
		}

		.form-item-input {
			text-align: right;
		}
	}

	.zy {
		font-size: 30rpx;
	}

	.zu-text {
		margin-top: 15rpx;
		line-height: 1.8em;
	}

	.pay {
		width: 90%;
		height: 76rpx;
		line-height: 76rpx;
		background-color: #00a6f8;
		margin: 200rpx auto 100rpx;
		border-radius: 10rpx;
		text-align: center;
		font-size: 34rpx;
		color: #ffffff;
	}
</style>
